package com.example.myapplication.apter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Caringwelfare.BenefitActivity;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.BenefitBean;
import com.example.myapplication.ShuJu.BotYIzhanBean;
import com.example.myapplication.teenager.Hostel_DetailActivity;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;

import okhttp3.RequestBody;
import okhttp3.Response;

public class BenefitApter  extends RecyclerView.Adapter<BenefitApter.MyHolder>{
    private Context context;
    private List<BenefitBean.RowsBean> list;
    private static  final  MediaType  JSON = MediaType.parse("application/json; charset=utf-8");
    private Handler handler ;

    public BenefitApter(Context context, List<BenefitBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public BenefitApter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_benefit,null);
        return new BenefitApter.MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BenefitApter.MyHolder holder, int position) {
        if(list.get(position)==null){
            return;
        }
        Glide.with(context).load(TokenBean.IP+list.get(position).getImgUrl()).into(holder.bene_img);
        holder.projectTypeSpinner.setText("发布人:"+list.get(position).getAuthor());
        holder.publisherTextView.setText("项目类型:"+list.get(position).getName());
        holder.fundraisedTextView.setText("已筹善款:"+list.get(position).getMoneyNow());
        holder.charityCategorySpinner.setText("公益类别:"+list.get(position).getType().getName());
        holder.projectTimeTextView.setText("项目时间:"+list.get(position).getActivityAt());
        holder.donorsTextView.setText("参捐人数:"+list.get(position).getDonateCount());
        //发起捐款
        holder.donateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               sendmoey(holder.ed_money,position);
            }
        });

    }

    private void sendmoey(EditText ed ,Integer i) {
        String ed_money = ed.getText().toString();
        JSONObject jsonObject = new JSONObject();
        handler = new Handler();
        try {
            jsonObject.put("activityId",String.valueOf(list.get(i).getId()));
            jsonObject.put("donateMoney",ed_money);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody  requestBody =  RequestBody.create( jsonObject.toString(),JSON);
        Request request =  new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/public-welfare/donate-record")
                .addHeader("Authorization", TokenBean.token)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "捐款成功", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        TextView publisherTextView;
        TextView fundraisedTextView;
        TextView charityCategorySpinner;
        TextView projectTypeSpinner;
        TextView charityContentTextView;
        TextView projectTimeTextView;
        TextView donorsTextView;
        Button donateButton;
        ImageView bene_img;
        EditText ed_money;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            projectTypeSpinner = itemView.findViewById(R.id.projectTypeSpinner);
            bene_img = itemView.findViewById(R.id.bene_img);
            charityCategorySpinner = itemView.findViewById(R.id.charityCategorySpinner);
            charityContentTextView = itemView.findViewById(R.id.charityContentTextView);
            publisherTextView = itemView.findViewById(R.id.publisherTextView);
            fundraisedTextView = itemView.findViewById(R.id.fundraisedTextView);
            projectTimeTextView = itemView.findViewById(R.id.projectTimeTextView);
            donorsTextView = itemView.findViewById(R.id.donorsTextView);
            donateButton = itemView.findViewById(R.id.donateButton);
            ed_money = itemView.findViewById(R.id.ed_money);
        }
    }


}



